#pragma once

#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include "Log.hpp"
#include "Err.hpp"

static const int defaultSock = -1;
static const int gbacklog = 32;

using namespace std;

class Sock{
public:
    Sock():_sock(defaultSock){

    }

    void Socket(){
        _sock = socket(AF_INET, SOCK_STREAM, 0);
        if(_sock < 0){
            logMessage(Fatal, "socket error, code: %d, errstring: %s", errno, strerror(errno));
            exit(SOCKET_ERR);
        }
    }

    void Bind(const uint16_t& port){
        struct sockaddr_in local;
        memset(&local, 0, sizeof(local));
        local.sin_addr.s_addr = INADDR_ANY;
        local.sin_family = AF_INET;
        local.sin_port = htons(port);

        if(bind(_sock, (struct sockaddr*)&local, sizeof(local)) < 0){
            logMessage(Fatal, "bind error, code: %d, errstring: %s", errno, strerror(errno));
            exit(BIND_ERR);
        }
    }

    void Listen(){
        if(listen(_sock, gbacklog) < 0){
            logMessage(Fatal, "listen error, code: %d, errstring: %s", errno, strerror(errno));
            exit(LISTEN_ERR);
        }
    }

    int Accept(string* clientIp, uint16_t* clientPort){
        struct sockaddr_in temp;
        socklen_t len = sizeof(temp);
        int sock = accept(_sock, (struct sockaddr*)&temp, &len);
        if(sock < 0)
            logMessage(Warning, "accept error, code: %d, errstring: %s", errno, strerror(errno));
        else{
            *clientIp = inet_ntoa(temp.sin_addr);
            *clientPort = ntohs(temp.sin_port);
        }
        return sock;
    }

    int Connect(const string& serverIp, const uint16_t& serverPort){
        struct sockaddr_in server;
        memset(&server, 0, sizeof(server));
        server.sin_addr.s_addr = inet_addr(serverIp.c_str());
        server.sin_family = AF_INET;
        server.sin_port = htons(serverPort);

        return connect(_sock, (struct sockaddr*)&server, sizeof(server));
    }
    
    int GetSock(){
        return _sock;
    }

    void Close(){
        if(_sock != defaultSock)
            close(_sock);
    }

    ~Sock(){
        if(_sock != defaultSock)
            close(_sock);
    }

private:
    int _sock;
};